package ink.tiptop.learn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ink.tiptop.learn.convert.OrderInfoConvert;
import ink.tiptop.learn.mapper.OrderInfoMapper;
import ink.tiptop.learn.mapper.UserBaseInfoMapper;
import ink.tiptop.learn.pojo.CommonResult;
import ink.tiptop.learn.convert.test.UserOrderBO;
import ink.tiptop.learn.pojo.dto.orderInfo.*;
import ink.tiptop.learn.pojo.po.OrderInfoPO;
import ink.tiptop.learn.pojo.po.UserBaseInfoPO;
import ink.tiptop.learn.pojo.vo.OrderInfoVO;
import ink.tiptop.learn.service.OrderInfoService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static ink.tiptop.learn.pojo.CommonResult.success;

/**
 * (OrderInfo)表服务实现类
 *
 * @author zjf
 * @since 2020-08-14 20:54:32
 */
@Service("orderInfoService")
public class OrderInfoServiceImpl implements OrderInfoService {
    @Resource
    private OrderInfoMapper orderInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CommonResult queryById(Integer id) {
        return success(this.orderInfoMapper.selectById(id));
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public CommonResult deleteById(Integer id) {
        return success(this.orderInfoMapper.deleteById(id) > 0);
    }

    @Resource
    private OrderInfoConvert orderInfoConvert;
    @Override
    public CommonResult list(QueryOrderInfoDto request) {
        // 转换查询实体对象
        OrderInfoPO convert = orderInfoConvert.convert(request);
        // 查询
        List<OrderInfoPO> OrderInfoPOS = orderInfoMapper.selectList(new QueryWrapper<>(convert));
        // 转换vo
        List<OrderInfoVO> OrderInfoVOS = orderInfoConvert.convertList(OrderInfoPOS);
        return success(OrderInfoVOS);
    }

    public CommonResult update(UpdateOrderInfoDto request) {
        Integer count = orderInfoMapper.selectCount(new QueryWrapper<>(
                new OrderInfoPO().setId(request.getId())));

        if (count == null || count < 1) {
            return CommonResult.FAILED;
        }
        OrderInfoPO convert = orderInfoConvert.convert(request);

        int rows = 0;
        try {
            rows = orderInfoMapper.updateById(convert);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return CommonResult.FAILED;
            }
        }
        if (rows > 0) {
            return CommonResult.SUCCESS;
        }
        return CommonResult.FAILED;
    }


    public CommonResult delete(DeleteOrderInfoDto request) {
        int rows = orderInfoMapper.deleteBatchIds(request.getId());
        if (rows > 0) {
            return CommonResult.SUCCESS;
        }
        return CommonResult.FAILED;
    }

    public CommonResult add(AddOrderInfoDto request) {
        OrderInfoPO convert = orderInfoConvert.convert(request);
        int rows = orderInfoMapper.insert(convert);
        if (rows > 0) {
            return CommonResult.SUCCESS;
        }
        return CommonResult.FAILED;
    }

    @Resource
    private UserBaseInfoMapper baseInfoMapper;

    @Override
    public CommonResult userOrder(CustomerOrderInfoDto request) {
        UserBaseInfoPO userBaseInfoPO = baseInfoMapper.selectById(request.getUserId());
        OrderInfoPO orderInfoPO = orderInfoMapper.selectById(request.getId());
//        UserOrderBO userOrderBO = orderInfoConvert.convert2UserOrder(orderInfoPO, userBaseInfoPO);
        UserOrderBO userOrderBO = new UserOrderBO();
        orderInfoConvert.baseConvert(orderInfoPO,userOrderBO);
        return success(userOrderBO);
    }
}